package com.cib.record.service;


import com.cib.record.bean.Audio;
import com.cib.record.mapper.AudioMapper;
import com.cib.record.util.ShellUtil;
import jdk.nashorn.tools.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.spec.OAEPParameterSpec;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @描述:
 * @Author: zhangh
 * @Time: 2021/5/12 15:51
 **/
@Service
public class AudioUpload {

    private static final Logger logger = LoggerFactory.getLogger(AudioUpload.class);

    @Resource
    AudioMapper audioMapper;

    @Autowired
    UploadService uploadFileService;

//    @Autowired
//    UploadUtil uploadUtil;

    @Value("${location.audio.path}")
    private String audio_path;

    @Value("${tar.path}")
    private String tar_path;

    @Value("${cur.hostip}")
    private String hostAddress;

    @Scheduled(cron ="${crontab.exp2}")
    public void work() throws UnknownHostException {
        System.out.println("进入方法specific");
       // Audio audio = audioMapper.selectByPrimaryKey(1);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String today = simpleDateFormat.format(date);
        List<Audio> audiolist=audioMapper.selectTodayAudio(today);

        //确定本地服务器上有是否有数据库中查询出的sessionId,如果有，将其记录到list中
        List<String> pathList=new ArrayList<>();



        for (Audio audio1 : audiolist) {
            //判断是否有该文件
            if (existFile(audio1.getSessionId())){
                String audioPath=audio1.getSessionId()+".wav";
                System.out.println("audioPath"+audioPath);
               pathList.add(audioPath);
            }

        }

        if (pathList.size()==0){
            logger.info("The required recording does not exist on the server");
            return;
        }else{

            //String md5file="md5sum 20210516.tar >\n"
            String md5file=tar_path+hostAddress+".md5";
         //   UploadUtil.executeShell(mkshell,"createDirShell");
           // String tarAbsolutName=tar_path+today+".tar";
            String tarAbsolutName=tar_path+hostAddress+".tar";
            //打包当天录音
            String tarshellStr="tar -cvPf "+tarAbsolutName+" "+"-C"+" "+audio_path;
            for (int i = 0; i < pathList.size(); i++) {
                tarshellStr=tarshellStr+" "+pathList.get(i);
            }
            System.out.println(tarshellStr);
            logger.info(tarshellStr);
            ShellUtil.executeShell(tarshellStr,"tarshell");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //生成一个ip为名字，后缀为“.md5的文件”

            String md5shell="md5sum "+tarAbsolutName+" "+">"+md5file;
            ShellUtil.executeShell(md5shell,"createMd5file");
            uploadFileService.uploadFile(md5file);
           uploadFileService.uploadFile(tarAbsolutName);

        }



    }

    private boolean existFile(String sessionId) {
        //拼接录音文件路径
        String audioPath=audio_path+sessionId+".wav";
        System.out.println(audioPath);
        File file = new File(audioPath);
        if (file.isFile()){
            return true;
        }else{
            return false;
        }
    }
}
